﻿@{
    ViewBag.Title = "用户管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
var userLastIndex;
var postdata =[];
var posts = eval("(@ViewBag.postJoson)".replace(/&#39;/g,"'"));        
var organizes = eval("(@ViewBag.organizeJson)".replace(/&#39;/g,"'"));

$(function(){
       //用户管理
			$('#user').datagrid({
				toolbar:[{
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                     $('#user').datagrid('reload');
                }
              },'-',{
					text:'添加',
					iconCls:'icon-add',
					handler:function(){
                        $("#nomsg").hide();
					    $('#user').datagrid('endEdit', userLastIndex);
						$('#user').datagrid('appendRow',{
                                Id : guidGenerator(),
                                UserId : '',
                                UserName : '',
                                Password : '',
                                OrganizeId : '',
                                PostId : '',
                                Email : '',
                                Address : ''
						});
						userLastIndex = $('#user').datagrid('getRows').length-1;
						$('#user').datagrid('selectRow', userLastIndex);
						$('#user').datagrid('beginEdit', userLastIndex);
					}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#user').datagrid('getSelected');
						if (row){
						   var index = $('#user').datagrid('getRowIndex', row);
                           $.messager.confirm('用户管理', '您确定要删除改记录吗?', function (r) {
                                            if (r) {
                                                var node = $('#user').datagrid('getSelected');
                                                if (node) {
                                                    $.ajax({
                                                      url:"/User/DeleteUserById?Id="+row.Id ,
                                                      data: node,
                                                      success: function(text){
                                                         $('#user').datagrid('deleteRow', index);
                                                         $.messager.show({
				                                            title:'用户管理',
				                                            msg:text,
				                                            timeout:1500,
				                                            showType:'slide'
			                                            });
                                                      }
                                                    });
                                                }
                                            }
                             });
						}
					}
				},'-',{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						$('#user').datagrid('acceptChanges');
					}
				},'-',{
					text:'撤销',
					iconCls:'icon-undo',
					handler:function(){
						$('#user').datagrid('rejectChanges');
					}
				}],
                onClickRow:function(rowIndex){
                     if (userLastIndex != rowIndex){
                      $('#user').datagrid('endEdit', userLastIndex);
                     }
                },
				onDblClickRow:function(rowIndex){
					if (userLastIndex != rowIndex){
						$('#user').datagrid('endEdit', userLastIndex);
						$('#user').datagrid('beginEdit', rowIndex);
					}
					userLastIndex = rowIndex;
				},
                onLoadSuccess:function(data){
                    if(data.total == 0)
                    {
                         $("#nomsg").show();
                    }
                    else
                    {
                        $("#nomsg").hide();
                    }
                }
                ,onAfterEdit:function(rowIndex, row, changes){
                     var rowdata = { "Id":row.Id
                             , "UserId":row.UserId
                             , "UserName":row.UserName
                             , "OrganizeId":row.OrganizeId
                             , "PostId":row.PostId 
                             , "Email":row.Email
                             , "Address":row.Address
                             };
                    $.ajax({
                        url:"@Url.Action("InsertOrEditUser", "User")" ,
                        data: rowdata,
                        success: function(text){
                                    $.messager.show({
				                        title:'用户管理',
				                        msg:text,
				                        timeout:1500,
				                        showType:'slide'
			                        });
                                }
                        });
                }
			});
});

$.extend($.fn.datagrid.defaults.editors, {   
    passwordInput: {   
        init: function(container, options){   
            var input = $('<input type="password" class="datagrid-editable-input"/>').appendTo(container);   
            return input;   
        },   
        getValue: function(target){   
            return $(target).val(); 
        },   
        setValue: function(target, value){   
			$(target).val(value);
        },   
        resize: function(target, width){   
            var input = $(target);   
            if ($.boxModel == true){   
                input.width(width - (input.outerWidth() - input.width()));   
            } else {   
                input.width(width);   
            }   
        }   
    }   
}); 

$.extend($.fn.datagrid.defaults.editors, {   
    comboTree: {   
        init: function(container, options){   
            var input = $('<input class="easyui-combotree" style="width:160px;" required="true"/>').appendTo(container);   
            return input.combotree({
            url:"@Url.Action("GetOrganizeTree", "Organize")",
            onChange: function(newValue, oldValue){
                loadPostData(newValue);
            }
            });   
        },   
        getValue: function(target){   
            return $(target).combotree('getValue'); 
        },   
        setValue: function(target, value){   
			$(target).combotree('setValue', value);
//            loadPostData(value);
        },   
        resize: function(target, width){   
            var input = $(target);   
            if ($.boxModel == true){   
                input.width(width - (input.outerWidth() - input.width()));   
            } else {   
                input.width(width);   
            }   
        }   
    }   
}); 

$.extend($.fn.datagrid.defaults.editors, {   
    comboBox: {   
        init: function(container, options){   
            var row = $('#user').datagrid('getSelected');
            var input = $('<input id="comboBox'+row.Id+'"  style="width:160px;" required="true"/>').appendTo(container);   
            return input.combobox({
            valueField:'Id',
            textField:'PostName'
            });   
        },   
        getValue: function(target){   
            return $(target).combobox('getValue'); 
        },   
        setValue: function(target, value){   
			$(target).combobox('setValue', value);
        },   
        resize: function(target, width){   
            var input = $(target);   
            if ($.boxModel == true){   
                input.width(width - (input.outerWidth() - input.width()));   
            } else {   
                input.width(width);   
            }   
        }   
    }   
}); 

function loadPostData(organizeId){
    postdata = [];
    var pdIndex = 0;
    for(var i=0; i<posts.length; i++){
		if (posts[i].OrganizeId.toLowerCase() == organizeId.toLowerCase())
        {
            postdata[pdIndex] = {Id:posts[i].Id,PostName:posts[i].PostName};
            pdIndex++;
        }
    }
    var row = $('#user').datagrid('getSelected');
    $("#comboBox"+row.Id).combobox("clear");
    $("#comboBox"+row.Id).combobox("loadData",postdata);
//    if(postdata.length > 0)
//        $("#comboBox"+row.Id).combobox("setValue",postdata[0].Id);
}

function organizeFormatter(value){
    for(var i=0; i<organizes.length; i++){
				if (organizes[i].Id == value) return organizes[i].OrganizeName;
	}
    return value;
}

function passwordFormatter(value){
    return "******";
}

function postFormatter(value){
    for(var i=0; i<posts.length; i++){
				if (posts[i].Id == value) return posts[i].PostName;
	}
    return value;
}

function permisionFormatter(value,rec){
    var rtnStr =""
    if(rec.PostId == '')
    {
        rtnStr = "<a href='javascript:void(0);' onclick='alert(\"请先保存！\");'>重置密码</a> &nbsp;";
        rtnStr += "<a href='javascript:void(0);' onclick='alert(\"请先保存！\");'>设置</a>";
    }
    else
    {
        rtnStr = "<a href='javascript:void(0);' onclick=\"reSetPassword('"+value+"');\">重置密码</a> &nbsp;";
		rtnStr += "<a href='javascript:void(0);' onclick=\"openPermision('/Permision/Index?PostId="+ rec.PostId +"&UserId="+ value +"&Type=User','permision',600,600);\">设置</a>";
    }
    return rtnStr
}

function openPermision(url)
{
    $('#openXXXIframe')[0].src = 'abount:blank';
    $('#openXXXIframe')[0].src=url; 
    $('#openPermisionDiv').dialog('open'); 
}

function reSetPassword(userId)
{
    $.ajax({
        url:"/User/ReSetPassword?Id="+userId ,
        data: null,
        success: function(text){
            $.messager.show({
			title:'用户管理',
			msg:text,
			timeout:1500,
			showType:'slide'
		});
        }
    });
}

</script>
<table id="user" title="用户管理" iconcls="icon-edit" singleselect="true" fit="true" pagination="true"
    rownumbers="true" idfield="Id" url="@Url.Action("ALLUserJson", "User")">
    <thead>
        <tr>
            <th field="UserId" width="80" editor="text" required="true">
                账号
            </th>
            <th field="UserName" width="80" editor="text">
                姓名
            </th>
@*            <th field="Password" width="80" editor="passwordInput" formatter="passwordFormatter">
                密码
            </th>*@
            <th field="OrganizeId" width="160" editor="comboTree" formatter="organizeFormatter">
                部门
            </th>
            <th field="PostId" width="160" formatter="postFormatter" editor="comboBox" >
                岗位
            </th>
            @*editor="{type:'combobox',options:{valueField:'Id',textField:'PostName',data:postdata,required:true}}"*@
            <th field="Email" width="120" editor="text">
                Email
            </th>
            <th field="Address" width="200" editor="text">
                地址
            </th>
            <th field="Id" width="120" formatter="permisionFormatter">
                权限
            </th>
        </tr>
    </thead>
</table>
<div id="nomsg" style="display: none; position: absolute; top: 85px; left: 5px; border-bottom: 1px #EEE dotted;
    border-right: 1px #EEE dotted; color: #999; width: 269px; padding-left: 160px;
    padding-bottom: 5px; padding-top: 5px">
    没有查询到相关数据!
</div>
<div id="openPermisionDiv" class="easyui-window" closed="true" modal="true" title="权限树"
    style="width: 600px; height: 600px;">
    <iframe scrolling="auto" id='openXXXIframe' frameborder="0" src="" style="width: 100%;
        height: 100%;"></iframe>
</div>
